package cn.example.zjgsu.service.Impl;

import cn.example.zjgsu.dao.CommentMapper;
import cn.example.zjgsu.dao.FocusUserMapper;
import cn.example.zjgsu.dao.FoodRecordMapper;
import cn.example.zjgsu.dao.UserMapper;
import cn.example.zjgsu.exception.MyException;
import cn.example.zjgsu.pojo.result.ResultMFR;
import cn.example.zjgsu.service.DiscoverPageServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: miaopeihan
 * @date: 2020-03-18  19:02
 */
@Service
public class DiscoverPageServerImpl implements DiscoverPageServer {

    @Autowired
    UserMapper userMapper;

    @Autowired
    FoodRecordMapper foodRecordMapper;

    @Autowired
    FocusUserMapper focusUserMapper;

    @Autowired
    CommentMapper commentMapper;

    @Override
    public List<ResultMFR> getFocusFoodRecord(String account) throws MyException {
        List<String> focusAccount=focusUserMapper.getFocus(account);
        if (focusAccount.size()==0){
            throw new MyException("你还没有关注哦,快去关注一点其他人吧");
        }
        List<ResultMFR> result=new ArrayList<ResultMFR>();
        for (String acount : focusAccount) {
            List<ResultMFR> resultMFRS = foodRecordMapper.getBriefFoodRecordByUserId(acount);
            if (resultMFRS.size()!=0){
                for (ResultMFR resultMFR : resultMFRS) {
                    String[] split = resultMFR.getPictures().split(";");
                    resultMFR.setPictures(split[0]);
                    resultMFR.setCommentnumber(commentMapper.getCommentNumberOfFD(resultMFR.getId()));
                    result.add(resultMFR);
                }
            }
        }
        if (result.size()==0){
            throw  new MyException("你关注的人还没有发食记哦！！！");
        }
        return result;
    }

    @Override
    public List<ResultMFR> getPopularFoodRecord() {
        List<ResultMFR> theBestFoodRecord = foodRecordMapper.getTheBestFoodRecord();
        for (ResultMFR resultMFR : theBestFoodRecord) {
            String[] split = resultMFR.getPictures().split(";");
            resultMFR.setPictures(split[0]);
            resultMFR.setCommentnumber(commentMapper.getCommentNumberOfFD(resultMFR.getId()));
        }
        return theBestFoodRecord;
    }

    @Override
    public void addViews(int foodRecordId) {
        foodRecordMapper.updateViewnumberById(foodRecordId);
    }

    /*SplitPictures splitPictures = new SplitPictures();


    @Override
    public List<ResultMainFoodRecord> getPopularFoodRecord() {
        List<FoodRecord> foodRecords = foodRecordMapper.getTheBestFoodRecord();
        List<ResultMainFoodRecord> result=new ArrayList<ResultMainFoodRecord>();
        for(FoodRecord foodRecord:foodRecords){
            ResultMainFoodRecord fr=new ResultMainFoodRecord();
            ResultFoodRecord resultFoodRecord = new ResultFoodRecord(foodRecord);
            resultFoodRecord.setPictures(splitPictures.getSplitPictures(foodRecord.getPictures()));
            fr.setResultFoodRecord(resultFoodRecord);
            User user = userMapper.selectByPrimaryKey(foodRecord.getUserid());
            fr.setUserId(user.getAcount());
            fr.setHeadportrait(user.getHeadportrait());
            fr.setName(user.getName());
            result.add(fr);
        }
        return result;
    }

    @Override
    public List<ResultMainFoodRecord> getFocusFoodRecord(String account) {
        List<String>focusAccount=focusUserMapper.getFocus(account);
        List<ResultMainFoodRecord> result=new ArrayList<ResultMainFoodRecord>();
        for(String userId:focusAccount){
            User user = userMapper.selectByPrimaryKey(userId);
            List<FoodRecord> foodRecords = foodRecordMapper.selectByUserId(userId);
            for(FoodRecord foodRecord:foodRecords){
                ResultMainFoodRecord fr=new ResultMainFoodRecord();
                fr.setUserId(user.getAcount());
                fr.setHeadportrait(user.getHeadportrait());
                fr.setName(user.getName());
                ResultFoodRecord resultFoodRecord = new ResultFoodRecord(foodRecord);
                resultFoodRecord.setPictures(splitPictures.getSplitPictures(foodRecord.getPictures()));
                fr.setResultFoodRecord(resultFoodRecord);
                result.add(fr);
            }
        }
        return result;
    }*/


}
